package com.demo.service;

import com.demo.bean.User;
import com.demo.dao.PieceDao;
import com.demo.util.MidUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PieceService {

    @Autowired
    PieceDao dao;

    //    获取分页列表
    public Map<String,Object> getPageList(Map<String, Object> paramMap) {
        Long totalCount=dao.getTotalCount(paramMap);
        List<Map<String,Object>> data=dao.getMapListByParam(paramMap);

        Map<String,Object>resultMap=new HashMap<>();
        resultMap.put("data",data);
        resultMap.put("count",totalCount);
        resultMap.put("code",0);
        resultMap.put("msg","ok");
        return resultMap;
    }
    //保存数据，自动根据有无id判断是新增还是更新
    public void saveItem(Map item) {
        //获取项目路径
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        System.out.println(jarF.getParentFile().toString());
        String uploadPath=jarF.getParentFile().toString();
        String note=item.get("note").toString();
        String[] noteArr={};
        if(note.contains("\n")){
            noteArr=note.split("\n");
        }else {
            noteArr=new String[]{note};
        }

        String pitch=item.get("pitch").toString();
        String[] pitchArr={};
        if(pitch.contains("\n")){
            pitchArr=pitch.split("\n");
        }else {
            pitchArr=new String[]{pitch};
        }
        String meter=item.get("meter").toString();
        String[] meterArr={};
        if(meter.contains("\n")){
            meterArr=meter.split("\n");
        }else {
            meterArr=new String[]{meter};
        }
        String component=item.get("component").toString();
        String[] componentArr={};
        if(note.contains("\n")){
            componentArr=component.split("\n");
        }else {
            componentArr= new String[]{component};
        }
        String name=item.get("name").toString();
        long timestamp = System.currentTimeMillis();
        String midPath=uploadPath+File.separator+"audio"+File.separator+timestamp+"-"+name+".mid";
        String wavPath=uploadPath+File.separator+"audio"+File.separator+timestamp+"-"+name+".wav";

        MidUtil.genMid(noteArr,pitchArr,meterArr,componentArr,item.get("tone").toString(),midPath);
        MidUtil.mid2Wav(midPath,wavPath);

        String id = item.get("id").toString();
        item.put("createtime",new Date());
        item.put("url",timestamp+"-"+name+".wav");
        if(StringUtils.isEmpty(id)){
            dao.addItem(item);

        }else {
            dao.updateItem(item);
        }

    }


    public List<Map> getAll(String userId) {
        return dao.getAll(userId);
    }

    public User getById(String id) {
        return dao.getById(id);
    }

    public void deleteItemsByIds(Map param) {
        System.out.println(param);
    }

    public String preview(Map item) {
        //获取项目路径
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        System.out.println(jarF.getParentFile().toString());
        String uploadPath=jarF.getParentFile().toString();
        String note=item.get("note").toString();
        String[] noteArr={};
        if(note.contains("\n")){
            noteArr=note.split("\n");
        }else {
            noteArr=new String[]{note};
        }

        String pitch=item.get("pitch").toString();
        String[] pitchArr={};
        if(pitch.contains("\n")){
            pitchArr=pitch.split("\n");
        }else {
            pitchArr=new String[]{pitch};
        }
        String meter=item.get("meter").toString();
        String[] meterArr={};
        if(meter.contains("\n")){
            meterArr=meter.split("\n");
        }else {
            meterArr=new String[]{meter};
        }
        String component=item.get("component").toString();
        String[] componentArr={};
        if(note.contains("\n")){
            componentArr=component.split("\n");
        }else {
            componentArr= new String[]{component};
        }
        String name=item.get("name").toString();
        long timestamp = System.currentTimeMillis();
        String midPath=uploadPath+File.separator+"audio"+File.separator+timestamp+"-"+name+".mid";
        String wavPath=uploadPath+File.separator+"audio"+File.separator+timestamp+"-"+name+".wav";

        MidUtil.genMid(noteArr,pitchArr,meterArr,componentArr,item.get("tone").toString(),midPath);
        MidUtil.mid2Wav(midPath,wavPath);
        return timestamp+"-"+name;
    }
}
